yjx.controller('teamController', ['$rootScope', '$scope', '$fetch', 'modalService', '$timeout', 'wxShareService', 'ipCookie', '$stateParams', 'detailService', function ($rootScope, $scope, $fetch, modalService, $timeout, wxShareService, ipCookie, $stateParams, detailService) {
    var teamNo = $stateParams.teamNo;
    var forWardId = $stateParams.id;
    $scope.joinTeam = false;
    if (teamNo && forWardId) {
        $rootScope.teamJoin = false;
        $scope.joinTeam = true;
        $rootScope.teamNo = teamNo;
        $rootScope.inviterUserId = forWardId;
        detailService.openDetail('joinTeam');
    }
    /**swiper */
    var tabList, teamSwiper;
    var width = document.body.offsetWidth;
    $scope.tabList = [{ id: 1, name: '我创建的' }, { id: 2, name: '我加入的' }];
    $scope.curTab = $scope.tabList[0];  
    function buildSwiper() {
        tabList = new Swiper('#tabList', {
            slidesPerView: 2,
            slideToClickSlide: true
        });
        teamSwiper = new Swiper('#teamSwiper', {
            direction: 'horizontal',
            // noSwiping : true,
            slider: width,
            on: {
                slideNextTransitionStart: function () {
                    tabList.slideTo(teamSwiper.activeIndex)
                    $timeout(function () {
                        $scope.curTab = $scope.tabList[teamSwiper.activeIndex];
                    })
                },
                slidePrevTransitionStart: function () {
                    tabList.slideTo(teamSwiper.activeIndex - 1)
                    $timeout(function () {
                        $scope.curTab = $scope.tabList[teamSwiper.activeIndex];
                    })
                }
            }
        })
    }
    $scope.tabSelect = function (item, index) {
        $scope.curTab = item;
        teamSwiper.slideTo(index, 500);
    }
    $timeout(function () {
        buildSwiper();
        // $timeout(
        //     function () {
        //         if (forWardId && !teamNo) {
        //             $rootScope.changeState = true;
        //             $rootScope.changeStateName = 'user';  
        //             $scope.tabSelect($scope.tabList[1], 1);
        //         }
        //     }
        // )
    }, 1000)

    function getTeamInfo() {
        $fetch.fetch(
            '/group/detail/info',
            {
                params: {},
                cache: false
            }
        ).then(function (data) {
            $scope.teamC = data;
            if ($scope.teamC.no) {
                wxShareInt();
            }
        }).catch(function(data){
            
        })
    }
    getTeamInfo();

    function getTeamJInfo() {
        $fetch.fetch(
            '/group/detail/info',
            {
                params: {
                    type: false
                },
                cache: false
            }
        ).then(function (data) {
            $scope.teamJ = data;
        })
    }
    getTeamJInfo();
    /**邀请加入团队 */

    $scope.invite = function () {

        modalService.mainMsgInfo("点击右上角将邀请链接发给客户");
    }
    $scope.teamC = {};
    $rootScope.userId = $rootScope.userId ? $rootScope.userId : ipCookie('userId');
    function wxShareInt() {
        var desc = "邀请您加入我的基因团队";
        var link;
        // if ($rootScope.isAndroid) {
        //     link = APPSRC+"?inviteTeamId="+$rootScope.userId;
        // }
        // else {
        //     link = APPSRC+"#/?inviteTeamId="+$rootScope.userId;
        // }
        link = APPSRC + "#/team?id=" + $rootScope.userId + "&teamNo=" + $scope.teamC.no+"&identity=1";
        wxShareService.onMenuShareAppMessage('', desc, '', link, '');
        wxShareService.onMenuShareTimeline('', desc, '', link, '');
    }

}])